home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / D-G / DA Shell.cpt / DA Shell ƒ / DA Shell.c next >
Encoding:
C/C++ Source or Header  |  1989-10-29  |  8.1 KB  |  480 lines  |  [TEXT/KAHL]

  1.  
  2. /*
  3.     DA Shell D/A  --  Version 1.00  --  Oct 29, 1989
  4.     
  5.     Copyright (c) 1989 by Neal E. Trautman
  6.     
  7.     'ShareWare' -- Please send $5 contribution to:
  8.  
  9.         Neal Trautman
  10.         1701 S.W. 42nd Street
  11.         Fargo, ND  58103
  12.     
  13.     This is a D/A Shell.
  14. */
  15.  
  16. #define date    "\p10/29/89"
  17. #define version    "\pv1.00"
  18.  
  19. #define line1 "\p by Neal E. Trautman"
  20. #define line2 "\p'ShareWare'"
  21. #define line3 "\pPlease send $5 contribution to:"
  22. #define line4 "\pNeal Trautman"
  23. #define line5 "\p1701 S.W. 42nd Street"
  24. #define line6 "\pFargo, ND  58103"
  25. #define line7 version
  26. #define line8 date
  27.  
  28. typedef enum {
  29.     ABOUT = 1,
  30.     ULINE,
  31.     QUIT
  32. } Command;
  33.  
  34. typedef    unsigned long    ulong;
  35.  
  36. #define FAST         register
  37.  
  38. #define NULL         0L
  39. #define BUTTON_OFF    0
  40. #define BUTTON_ON    1
  41. #define BUTTON_DIM    255
  42.  
  43. #define    pstrlen(ps)            (int)(ps[0])
  44. #define    pcharcat(ps,ch)        ps[++(ps[0])] = ch
  45.  
  46. #define MENU        1
  47.  
  48. #define CONTENT_H    100
  49. #define CONTENT_V    100
  50.  
  51. #define INSET        4
  52. #define BUTTON_H    80
  53. #define BUTTON_V    20
  54. #define BOTTOM        (INSET+CONTENT_V+INSET)
  55.  
  56. #define WINDOWWIDTH     (INSET+CONTENT_H+INSET)
  57. #define WINDOWHEIGHT     (BOTTOM+INSET+BUTTON_V+INSET)
  58. #define WINDOWTOP        40
  59. #define WINDOWLEFT        2
  60.  
  61. Boolean        already_open = FALSE;
  62. DCtlPtr     dce;
  63. Rect        screenBounds;
  64. WindowPtr     window;
  65. Rect        rct;
  66. int            linev,lineinc;
  67. int         line_height;
  68. Str255        daname;
  69. ControlHandle     aboutB    = NULL;
  70. MenuHandle        menu = NULL;
  71.  
  72. static void pstrcpy(d, s)
  73. StringPtr    d, s;
  74. {
  75.     BlockMove(s, d, s[0] + 1);
  76. }
  77.  
  78. static void pstrcat(d, s)
  79. StringPtr    d, s;
  80. {
  81.     BlockMove(s+1, d+(d[0])+1, s[0]);
  82.     d[0] += s[0];
  83. }
  84.  
  85. static int da_id(id)
  86.     int        id;
  87. {
  88.     return((0xC000 + ((~dce->dCtlRefNum) << 5)) + id);
  89. }
  90.  
  91. static void doline(line)
  92.     StringPtr    line;
  93. {
  94.     MoveTo (rct.left + (rct.right - rct.left - StringWidth (line)) / 2, linev);
  95.     DrawString (line);
  96.     linev += lineinc;
  97. }
  98.  
  99. static void drawabout(w)
  100. WindowPtr    w;
  101. {
  102.     Str255    str;
  103.     
  104.     linev = 24;
  105.     lineinc = 14;
  106.     rct = w->portRect;
  107.     InsetRect (&rct, 4, 4);
  108.     TextFont (systemFont);
  109.     TextSize (12);
  110.     pstrcpy(str,daname);
  111.     pstrcat(str,line1);
  112.     doline (str);
  113.     TextFont (geneva);
  114.     TextSize (9);
  115.     doline ("\p");
  116.     doline (line2);
  117.     doline (line3);
  118.     TextFace(bold);
  119.     doline (line4);
  120.     doline (line5);
  121.     doline (line6);
  122.     TextFace(0);
  123.     MoveTo (rct.left, rct.bottom);
  124.     DrawString (line7);
  125.     MoveTo (rct.right - StringWidth (line8), rct.bottom);
  126.     DrawString (line8);
  127.     TextFace(0);
  128. }
  129.  
  130. static void doAbout()
  131. {
  132.     register WindowPtr    w;
  133.     EventRecord    evt;
  134.     Boolean        done;
  135.     int            h,v;
  136.     Rect        r;
  137.  
  138.     InitCursor ();
  139.     SetRect (&r, 0, 0, 240, 120);
  140.     w = NewWindow (NULL, &r, "\p", FALSE, dBoxProc, -1L, FALSE, 0);
  141.     SetPort (w);
  142.     h = screenBounds.left + (((screenBounds.right - screenBounds.left) -
  143.         (w->portRect.right - w->portRect.left)) / 2);
  144.     v = screenBounds.top  + (((screenBounds.bottom - screenBounds.top) -
  145.         (w->portRect.bottom - w->portRect.top)) / 3);
  146.     MoveWindow (w, h, v, FALSE);
  147.     ShowWindow (w);
  148.     /* drawabout (w); */
  149.     done = FALSE;
  150.     while (!done)
  151.         {
  152.         SystemTask();
  153.         if (GetNextEvent (everyEvent, &evt))
  154.             switch (evt.what)
  155.                 {
  156.                 case mouseDown:            
  157.                     while (WaitMouseUp())
  158.                         ;        /* and fall thru... */
  159.                 case mouseUp:            
  160.                 case keyDown:
  161.                 case autoKey:
  162.                     done = TRUE;
  163.                     break;
  164.                 case updateEvt:
  165.                     if ((WindowPtr)(evt.message) == w)
  166.                         {
  167.                         SetPort (w);
  168.                         BeginUpdate (w);
  169.                         drawabout (w);
  170.                         EndUpdate (w);
  171.                         }
  172.                     break;
  173.                 }
  174.         }
  175.     DisposeWindow (w);
  176.     FlushEvents (mDownMask+mUpMask+keyDownMask+keyUpMask+autoKeyMask,0);
  177. }
  178.  
  179. static void flashbutton(ch)
  180.     ControlHandle    ch;
  181. {
  182.     long            ticks;
  183.  
  184.     HiliteControl(ch,BUTTON_ON);
  185.     Delay(8L,&ticks);
  186.     HiliteControl(ch,BUTTON_OFF);
  187. }
  188.  
  189. static void doUpdate()
  190. {
  191.     GrafPtr    savePort;
  192.     Rect            r;
  193.     
  194.     GetPort(&savePort);
  195.     SetPort(window);
  196.         
  197.     MoveTo(0,BOTTOM);
  198.     LineTo(window->portRect.right,BOTTOM);
  199.     
  200.     TextFont(systemFont);
  201.     TextSize(0);
  202.     TextFace(bold | underline);
  203.     MoveTo(INSET,BOTTOM / 2);
  204.     DrawString(daname);
  205.         
  206.     /* Draw Window's Contents */
  207.     
  208.     SetPort(savePort);
  209. }
  210.  
  211. static void doActivate(activate)
  212.     Boolean    activate;
  213. {
  214.     GrafPtr    savePort;
  215.     
  216.     GetPort(&savePort);
  217.     SetPort(window);
  218.     if (menu != NULL)
  219.         {
  220.         if (activate)
  221.             InsertMenu(menu,0);
  222.         else
  223.             DeleteMenu(dce->dCtlMenu);
  224.         DrawMenuBar();
  225.         }
  226.     InvalRect(&window->portRect);
  227.     SetPort(savePort);
  228. }
  229.  
  230. static void doCommand(option)
  231.     Command        option;
  232. {
  233.     Boolean        quit;
  234.     
  235.     quit = FALSE;
  236.     switch (option)
  237.         {
  238.         case ABOUT:
  239.             doAbout();
  240.             break;
  241.         case QUIT:
  242.             quit = TRUE;
  243.             break;
  244.         }
  245.     if (quit)
  246.         CloseDriver(dce->dCtlRefNum);
  247. }
  248.  
  249. static void doKey(c)
  250.     char    c;
  251. {
  252.     switch (c)
  253.         {
  254.         case '\r':
  255.         case '\03':
  256.         case '`':
  257.         case ' ':
  258.         case 'a':
  259.         case 'A':
  260.             flashbutton(aboutB);
  261.             doCommand(ABOUT);
  262.             break;
  263.         case 0x1B:
  264.         case 'q':
  265.         case 'Q':
  266.             doCommand(QUIT);
  267.             break;
  268.         default :
  269.             SysBeep(2);
  270.             break;
  271.         }
  272. }
  273.  
  274. static void doMouse(p)
  275.     Point    p;
  276. {
  277.     GrafPtr    savePort;
  278.     ControlHandle    theControl;
  279.     
  280.     GetPort(&savePort);
  281.     SetPort(window);
  282.     GlobalToLocal(&p);
  283.     FindControl(p, window, &theControl);        
  284.     if (theControl != NULL)
  285.         {
  286.         if (TrackControl(theControl, p, NULL ))
  287.             {
  288.             if (theControl == aboutB)
  289.                 {
  290.                 doCommand(ABOUT);
  291.                 }
  292.             }
  293.         }
  294.     SetPort(savePort);
  295. }
  296.  
  297. static void doEvent(evt)
  298.     register EventRecord    *evt;
  299. {
  300.     GrafPtr    savePort;
  301.  
  302.     switch (evt->what)
  303.         {
  304.         case updateEvt: 
  305.             GetPort(&savePort);
  306.             SetPort(window);
  307.             BeginUpdate(window);
  308.             EraseRect(&window->portRect);
  309.             DrawControls(window);
  310.             doUpdate();    
  311.             EndUpdate(window);
  312.             SetPort(savePort);
  313.             break;
  314.         case mouseDown:    
  315.             doMouse(evt->where);
  316.             break;
  317.         case activateEvt: 
  318.             doActivate((Boolean)(evt->modifiers & activeFlag));
  319.             break;
  320.         case keyDown: 
  321.         case autoKey:
  322.             doKey((char)(evt->message & charCodeMask));
  323.             break;
  324.     }
  325. }
  326.  
  327. static void doOpen(name)
  328.     StringPtr    name;
  329. {
  330.     GrafPort    myPort;
  331.     GrafPtr        savePort;
  332.     FontInfo     fInfo;
  333.     Rect        rect;
  334.     int            id,i;
  335.     Str255        str;
  336.     Handle        h;
  337.     char        s2[8];
  338.     
  339.     dce->dCtlFlags |= dNeedLock|dNeedGoodBye;
  340.     if (already_open)
  341.         {
  342.         SelectWindow(window);
  343.         return;
  344.         }
  345.     already_open = TRUE;
  346.     
  347.     GetPort(&savePort);
  348.  
  349.     OpenPort (&myPort);
  350.     screenBounds = myPort.portBits.bounds;
  351.     ClosePort (&myPort);
  352.     
  353.     SetRect(&rect, WINDOWLEFT, WINDOWTOP, 100, 100);
  354.     daname[0] = name[0]-1;
  355.     BlockMove(name+2, daname+1, daname[0]);
  356.     window = NewWindow( NULL, &rect, daname, FALSE, rDocProc, NULL, TRUE, NULL );
  357.     if (window)
  358.         {
  359.         ((WindowPeek)window)->windowKind = dce->dCtlRefNum;
  360.         dce->dCtlWindow = window;
  361.     
  362.         SetPort(window);
  363.         TextFont(geneva);
  364.         TextSize(9);
  365.         GetFontInfo(&fInfo);
  366.         line_height = fInfo.ascent+fInfo.descent+fInfo.leading;
  367.         
  368.         SizeWindow(window,WINDOWWIDTH,WINDOWHEIGHT,TRUE);
  369.         ShowWindow(window);
  370.         SelectWindow(window);
  371.         
  372.         SetRect(&rect, (WINDOWWIDTH / 2) - (BUTTON_H / 2), WINDOWHEIGHT-INSET-BUTTON_V,
  373.             (WINDOWWIDTH / 2) + (BUTTON_H / 2), WINDOWHEIGHT-INSET);
  374.         aboutB    = NewControl( window, &rect, "\pAbout…", TRUE,
  375.             BUTTON_OFF, BUTTON_OFF, BUTTON_ON, pushButProc, NULL );
  376.         
  377.         id = da_id(MENU);
  378.         menu = NewMenu(id,daname);
  379.         if (menu)
  380.             {
  381.             dce->dCtlMenu = id;
  382.             pstrcpy(str,"\pAbout ");
  383.             pstrcat(str,daname);
  384.             pstrcat(str,"\p…/`");
  385.             AppendMenu(menu,str);
  386.             AppendMenu(menu,"\p(-");
  387.             AppendMenu(menu,"\pQuit/Q");
  388.             /* InsertMenu(menu,0); */
  389.             }
  390.         }
  391.     SetPort(savePort);
  392. }
  393.  
  394. static void doControl(code, parm)
  395.     int    code;
  396.     int    *parm;
  397. {
  398.     switch(code)
  399.         {
  400.     case accEvent:
  401.         doEvent(*((EventRecord **)parm));
  402.         break;
  403.     case accMenu:
  404.         {
  405.         switch(parm[1])
  406.             {
  407.             case ABOUT:        /* About… */
  408.                 doCommand(ABOUT);
  409.                 break;
  410.             case ULINE:        /* (- */
  411.                 break;
  412.             case QUIT:        /* Quit */
  413.                 doCommand(QUIT);
  414.                 break;
  415.             }
  416.         HiliteMenu(0);
  417.         break;
  418.         }
  419.     case accUndo:
  420.     case accCut:
  421.     case accCopy:
  422.     case accPaste:
  423.     case accClear:
  424.         SysBeep(2);
  425.         break;
  426.     case accRun:
  427.         break;
  428.     case accCursor:
  429.         break;
  430.     }
  431. }
  432.  
  433. static void doClose()
  434. {
  435.     if (aboutB)
  436.         DisposeControl(aboutB);
  437.     if (window)
  438.         {
  439.         DisposeWindow(window);
  440.         window = NULL;
  441.         dce->dCtlWindow = NULL;
  442.         }
  443.     DeleteMenu(dce->dCtlMenu);
  444.     dce->dCtlMenu = 0;
  445.     DrawMenuBar();
  446. }
  447.  
  448. main(p, d, n)
  449.     cntrlParam    *p;
  450.     DCtlPtr        d;
  451.     int            n;
  452. {    
  453.     if (d->dCtlStorage == NULL)
  454.         {
  455.         if (n == 0)
  456.             {
  457.             SysBeep(2);
  458.             CloseDriver(d->dCtlRefNum);
  459.             }
  460.         return(0);
  461.         }
  462.     dce = d;
  463.     dce->dCtlFlags &= ~dCtlEnable;
  464.     switch (n)
  465.         {
  466.         case 0:
  467.             doOpen(p->ioNamePtr);                           
  468.             break;    
  469.         case 2:
  470.             doControl(p->csCode, p->csParam); 
  471.             break;    
  472.         case 4:
  473.             doClose();
  474.             break;    
  475.         }
  476.     dce->dCtlFlags |= dCtlEnable;
  477.     return(0);
  478. }
  479.  
  480.